;
!(function() {
    $.ajax({
        url: 'http://10.31.158.42/javascript/projectname/php/goodsdata.php',
        dataType: 'json',
        type: 'post',
    }).done(function(data) {
        let strhtml = '';
        let $contenthtml = $('.content p');
        let $num = getcookie('cookienum').split(',');
        let $id = getcookie('cookiesid').split(',');
        let $M = 0;
        let $S = 0;
        let $N = 0;
        let $price = 0;
        let $zonghe = 0;
        let $zonghe1 = 0;
        $.each($id, function(i, v) {
            $.each(data, function(index, value) {
                if (data[index].sid == v) {
                    strhtml += `<ul>
                <li class="select" style="width:100px; float:left; line-height:122px; text-align:center">
                    <input style="width: 20px;height:20px;outline: none" type="checkbox" name="" id="">
                </li>
                <li class="goods" style="width:400px; float:left;">
                    <img style="display:block;width:100px;height:100px;float:left;" src="${value.img}" alt="">
                    <span style="width:250px;padding:30px 10px; display: block;float: left;line-height: 20px;">${value.title}</span>
                </li>
                <li class="prices" style="width:100px; float:left;text-align:center;line-height:122px;">
                    ￥${value.price}
                </li>
                <li class="num" style="width:200px; float:left;text-align:center;line-height:122px;">
                    <i class="reduce" style="cursor: pointer;"><button style="width:22px;height:22px;">-</button></i>
                    <input class="shuliang11" type="text" style="text-align: center;" value="${$num[i]}">
                    <b class="add" style="cursor: pointer;"><button style="width:22px;height:22px;">+</button></b>
                </li>
                <li class="price sum" style="width:100px; float:left;text-align:center;line-height:122px;">
                    <i>${value.price*$num[i]}</i>
                </li>
                <div class="del" style="width:100px; float:left;height:122px;line-height:122px;cursor: pointer;">删除
                </div>
                </ul>`;
                    $S += value.price * v;
                    $N += Number(value.price * $num[i]);
                    $zonghe = Number(value.price * $num[i]);
                    $price = value.price;
                }
                $contenthtml.html(strhtml);
            });
            const $reduce = $('.content ul .num .reduce');
            const $add = $('.content ul .num .add');




            //删除商品
            const $sum = $('.content .sum');
            const $oul = $('.content p ul');
            $.each($oul, function(index, value) {
                let _this = this;
                $(this).on('click', 'div', function() {
                    $(_this).hide();
                    let $key = '';
                    let $name = '';
                    $key = $num.splice(i, 1);
                    //console.log($key);
                    $.cookie('cookienum', $key, { expires: 7 });
                    $name = $id.splice(i, 1);
                    //console.log($name)
                    $.cookie('cookiesid', $name, { expires: 7 });
                });
                var $firstnum = Number($(this).children().find('.shuliang11').val());
                $(this).on('click', 'button', function() {
                    $firstnum--;
                    if ($firstnum <= 0) {
                        $firstnum = 0;
                    }
                    $(_this).children().find('.shuliang11').val($firstnum);
                    $zonghe -= $price
                    $sum.html($zonghe);
                });

                $add.on('click', function() {
                    $firstnum++;
                    $(_this).children().find('.shuliang11').val($firstnum);
                    $zonghe += $price;
                    $sum.html($zonghe);
                });
            });
        });

        $('.bottom .shuliang').html($N);
        $('.bottom .zonghe').html($S);

        //全选
        const $selectall = $('.car .selectall input');
        const $selects = $('.select input');
        $selectall.on('click', function() {
            if ($selectall.prop('checked')) {
                $selects.prop('checked', true);
            } else {
                $selects.prop('checked', false);
            }
        });

        $selects.on('click', function() {
            if ($selects.size() === $('input:checked').length) {
                $selectall.prop('checked', true);
            } else {
                $selectall.attr('checked', false);
            }
        });

    });
})(jQuery);